package com.yx.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.Log;
import com.yx.common.base.BaseController;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.common.validator.ValidatorUtils;
import com.yx.common.validator.group.AddGroup;
import com.yx.common.validator.group.UpdateGroup;
import com.yx.modules.api.entity.TbUser;
import com.yx.system.entity.SysUser;
import com.yx.system.service.SysUserRoleService;
import com.yx.system.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wuye on 2017/11/27.
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<SysUser> pageUtil = new Page<SysUser>(query.getPage(), query.getLimit());
        Page<SysUser> page = sysUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public R info(){
        return R.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @Log("修改密码")
    @RequestMapping("/password")
    public R password(String password, String newPassword){
        Assert.isBlank(newPassword, "新密码不为能空");

        //sha256加密
        password = new Sha256Hash(password, getUser().getSalt()).toHex();
        //sha256加密
        newPassword = new Sha256Hash(newPassword, getUser().getSalt()).toHex();

        SysUser user = new SysUser();
        user.setUserId(getUserId());
        user.setPassword(newPassword);
        //更新密码
        boolean bFlag = sysUserService.updateById(user);
        if(!bFlag){
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") Long userId){
        SysUser user = sysUserService.selectById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @Log("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUser user){
        ValidatorUtils.validateEntity(user, AddGroup.class);
        user.setCreateTime(new Date());
        user.setCreateUserId(getUserId());
        sysUserService.save(user);
        List<Long> roles = user.getRoleIdList();
        for (Long role:roles){
            if (role == 3){
                return R.error("请选择正确流程创建作家");
            }
        }
        return R.ok();
    }


    /**
     * 修改用户
     */
    @Log("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUser user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        user.setCreateUserId(getUserId());
        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @Log("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds){
        if(ArrayUtils.contains(userIds, 1L)){
            return R.error("系统管理员不能删除");
        }

        if(ArrayUtils.contains(userIds, getUserId())){
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }
}
